/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.security;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.fujitsu.nanjing.tieba.view.velocity.tools.DomainTool;

/**
 * OnlineUserService.java
 * 
 * @author Administrator
 */
@Component
public class OnlineUserService {

	private static final List<String> users = new ArrayList<String>();
	private static final String 最高在线人数 = "highestOnLine";
	private static final String 最高在线人数时间 = "highestOnLineTime";
	private static int topUsers;
	private static String topUsersTime;
	private static DateFormat df;
	static {
		df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		topUsers = Integer.parseInt(new DomainTool().info(最高在线人数));
		topUsersTime = new DomainTool().info(最高在线人数时间);
	}
	public synchronized void saveOnlineUser(String username) {
		if (!getUsers().contains(username)) {
			getUsers().add(username);
			if (topUsers < getUsers().size()) {
				topUsers = getUsers().size();
				topUsersTime = format(new Date());
				HashMap<String, String> map = initMap();
				new DomainTool().write(map);
			}
		}

	}
	private HashMap<String, String> initMap() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(最高在线人数时间, topUsersTime);
		map.put(最高在线人数, String.valueOf(topUsers));
		return map;
	}
	public synchronized void deleteOnlineUser(String username) {
		getUsers().remove(username);
	}

	public static List<String> getUsers() {
		return users;
	}
	protected static String format(Date date) {

		return df.format(date);
	}

}
